﻿using System;
using System.Net;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Ayatta.Contract.Domain;
using Web.Base.Extensions;
using Web.Manage.Controllers;
using Web.Manage.Areas.Sys.Models;

namespace Web.Manage.Areas.Sys.Controllers
{
    public class FaqController : AdminBaseController
    {

        public FaqController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int page = 1, int size = 20, string title = null, byte? category = null, bool? showOnMenu = null)
        {
            var model = new FaqControllerModel.Index();
            var faqs = BaseService.GetPagedFaqs(page, size, title, category, showOnMenu).ToPagedList(page, size);
            model.Title = title;
            model.Category = category;
            model.ShowOnMenu = showOnMenu;
            model.Faqs = faqs;
            return PartialView(model);
        }


        public ActionResult Detail(int? param)
        {
            var model = new Faq();
            if (param.HasValue && param.Value > 0)
            {
                model = BaseService.GetFaq(param.Value);
            }
            return PartialView(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Detail(int? param, Faq model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (param.HasValue && param.Value > 0)
            {
                var old = BaseService.GetFaq(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = BaseService.UpdateFaq(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/sys/faq") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = BaseService.CreateFaq(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/sys/faq");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        [HttpPost]
        public ActionResult Delete(int? param)
        {
            if (param.HasValue && param.Value > 0)
            {
                var result = BaseService.DeleteFaq(param.Value);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/sys/faq") : Json(HttpStatusCode.BadRequest, "删除失败");
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }
    }
}
